package com.sw.test01.ui.activity;

import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.sw.test01.R;
import com.sw.test01.service.DownloadService;
import com.sw.test01.service.MyIntentService;
import com.sw.test01.service.MyService;

/**
 * 服务
 */
public class ServerActivity extends AppCompatActivity {
    private Toolbar tbToolbar;
    private Button btnStart;
    private Button btnStop;
    private Button btnIntent;
    private Button btnDownloadStart;
    private Button btnDownloadPause;
    private Button btnDownloadCancel;

    private static final String TAG = "ServerActivity";

    private DownloadService.DownloadBinder downloadBinder;

    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            downloadBinder = (DownloadService.DownloadBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_server);
        tbToolbar = (Toolbar) findViewById(R.id.tb_toolbar);
        btnStart = (Button) findViewById(R.id.btn_start);
        btnStop = (Button) findViewById(R.id.btn_stop);
        btnIntent = (Button) findViewById(R.id.btn_intent);
        btnDownloadStart = (Button) findViewById(R.id.btn_download_start);
        btnDownloadPause = (Button) findViewById(R.id.btn_download_pause);
        btnDownloadCancel = (Button) findViewById(R.id.btn_download_cancel);

        tbToolbar.setTitle("服务页");

        Intent intent = new Intent(this, DownloadService.class);
        startService(intent);
        bindService(intent, conn, BIND_AUTO_CREATE);

        if (ContextCompat.checkSelfPermission(ServerActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(ServerActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
        }

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ServerActivity.this, MyService.class);
                startService(intent);
            }
        });

        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ServerActivity.this, MyService.class);
                stopService(intent);
            }
        });

        btnIntent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "Thread id is " + Thread.currentThread().getId());
                Intent intent = new Intent(ServerActivity.this, MyIntentService.class);
                startService(intent);
            }
        });

        btnDownloadStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (downloadBinder == null){
                    return;
                }
                String url = "http://192.168.0.168:8080/download/uc.exe";
                downloadBinder.startDownlaod(url);
            }
        });

        btnDownloadPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (downloadBinder == null){
                    return;
                }
                downloadBinder.pauseDownload();
            }
        });

        btnDownloadCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (downloadBinder == null){
                    return;
                }
                downloadBinder.cancelDownload();
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 1:
                if (grantResults.length > 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(ServerActivity.this,"拒绝权限将无法使用程序",Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }
}
